package com.miyake.dsp.filter;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.ScilabExecute;

public class IirScilab implements Filter {
	private ComplexArray source;
	private FilterSetting setting;
	
	public IirScilab(FilterSetting setting) {	
		this.setting = setting;
	}

	private String getCommand() {
		ScilabCommand command = new ScilabCommand();
		String taps = command.getOrder(setting.getOrder());
		String ftype = command.getFilterType(setting.getFilterType());
		String cutoff = command.getCutoff(setting.getCutoff1(), setting.getCutoff2(), setting.getFrequency());
		String approx = command.getApprox(setting.getFilterAlgo());
		String delta = command.getDelta(setting.getDelta1(), setting.getDelta2());
		
		String cmd = "hz=iir(" + 
			taps + "," +
			ftype + "," +
			approx + "," +
			cutoff + "," +
			delta + ");";
		
		return cmd;
	}
	
	public ComplexArray getProcessed() {
		return ScilabExecute.execute(getCommand() + "yhz=flts(data1,hz)", source, "yhz");	
	}
	
	public double[] getMagnitude(int point) {
		String command2 = "[hzm,fr]=frmag(hz," + String.valueOf(point) + ");";
		return ScilabExecute.execute(getCommand() + command2, source, "hzm").getRealPart();
	}

	public double[] getPhase(int point) {
		String command2 =  "[hzm,fr]=frmag(hz,"+ String.valueOf(point) + ");pha=atan(hzm);";
		return ScilabExecute.execute(getCommand() + command2, source, "pha").getRealPart();
	}

	@Override
	public void setSource(ComplexArray source) {
		this.source = source;
	}

	@Override
	public ComplexArray getSource() {
		return this.source;
	}

	@Override
	public Object getSetting() {
		return setting;
	}
	
}
